/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu   表单-select组件类
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-29
 * @param
 * opt{
 * 	'el': ''        // select标签
 * }
 * form_data="defaultNum=1&size=1&multiple=1&isMust=1&isChange=1&defaultValue=5454"
 * @example
 * var a = new STK.cpt.extend.formSelect(opt);
 */
$require('form.cFormTips')
$require('form.formSelect')
LJS.define('form.cFormSelect',function(ljs){
/*
 * $Import('module.form.formSelect');
$Import('cpt.tipMsg');
$Import('tpl.formSelectTpl');
 */
    var cFormTips = ljs.form.cFormTips;
    var formSelectTpl = {
        tip: '选一个吧',
        error: '您得选一个'
    };
    var formSelect = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // select标签
        that.el = opt.el;
        that.changeCallback = opt.changeCallback || function(){};
        // 解form_data序列获得全局属性对象
        var _formData = that.el.getAttribute('form_data');
        that.data = typeof _formData === 'string' ? $.queryToJson(_formData) : {};
        // 缺省选项,从0开始
        that.defaultNum = parseInt(that.data.defaultNum) || parseInt(that.data.defaultNum) === 0 ? parseInt(that.data.defaultNum) : null;
        // 可见项数
        that.size = parseInt(that.data.size) || 1;
        // 是否可以选择多项
        that.multiple = parseInt(that.data.multiple) || 0;
        // 是否是必选项
        that.isMust = parseInt(that.data.isMust) || 0;
        // 是否实时判断合法性
        that.isChange = parseInt(that.data.isChange) || 0;
        that.defaultValue = that.data.defaultValue;
        that.init();
    };
    formSelect.prototype = new ljs.form.formSelect;
    formSelect.prototype.init = function(){
        var that = this;
        that.tip = new cFormTips({
            'el': that.el
        });
        // 如果有默认选项,则将默认选项选中
        if (that.defaultNum !== null && that.el.options[that.defaultNum]) {
            that.el.options[that.defaultNum].selected = 'selected';
        }
        that.defaultValue?that.setSelectedByValue(that.defaultValue):that.setSelected(that.defaultNum);
        // 设置可见项数
        that.setSize(that.size);
        // 如果是多选,将多选功能开启或关闭
        that.setMultiple(that.multiple);
        that.change({
            callback: function(obj){
				if(that.isChange){
					that.check()? that.hide():that.showError()
				}
                if(that.changeCallback){
                    that.changeCallback(that.getSelectOption().getAttribute("value"))
                }
            }
        });
    };
    /**
     * 检测
     * @return Boolean true || false
     * 当为非必填项时,直接返回true
     */
    formSelect.prototype.check = function(){
        var that = this;
        if (!that.isMust) { return true;}
		if (that.getSelectOption().getAttribute("value")==0){
				return false;
			}
		return true;
        };
   
    // 显示提示层
    formSelect.prototype.showTip = function(){
        var that = this;
        that.tip.show(formSelectTpl.tip);
    };
    // 显示错误提示层
    formSelect.prototype.showError = function(){
        var that = this;
        that.tip.show(formSelectTpl.error,1);
    };
    // 隐藏层
    formSelect.prototype.hide = function(){
        var that = this;
        that.tip.hide();
    };
	formSelect.prototype.getValue = function(){
        return this.getSelectOption().getAttribute("value")
    };
	
    /**
     * 更新html标签的form_data序列
     * 同时更新全局属性对象that.data
     */
    formSelect.prototype.J2Q = function(){
        var that = this;
        var opt = {
            'size': that.size || 1,
            'defaultNum': parseInt(that.data.defaultNum) || null,
            'multiple': parseInt(that.multiple) || 0,
            'isMust': parseInt(that.isMust) || 0,
            'isChange': parseInt(that.data.isChange) || 0
        };
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    return formSelect;
});
